﻿// =================================================================== 
// 项目说明
//====================================================================
// SunCity@Copy Right 2006-2008
// 文件： Contents.cs
// 项目名称：SunCity_MS
// 创建时间：2015/3/18
// 负责人：Chris
// ===================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using EMS20.Entity;
using EMS20.IDataAccessLayer;

namespace EMS20.DataAccessLayer
{
    /// <summary>
    /// 数据层实例化接口类 dbo.Contents.
    /// </summary>
    public partial class ContentsDataAccessLayer : IContentsDataAccessLayer
    {
		#region 构造函数
		/// <summary>
		/// ??????
		/// </summary>
		public ContentsDataAccessLayer()
		{
		}
		#endregion

        #region -----------实例化接口函数-----------
		
		#region 添加更新删除
		/// <summary>
		/// 向数据库中插入一条新记录。
		/// </summary>
		/// <param name="_ContentsModel">Contents实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Create_ContentsInsert(ContentsEntity _ContentsModel)
		{
			string sqlStr="CreateUpdateDelete_ContentsEntity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@cnt_id",SqlDbType.Int),
			new SqlParameter("@cnt_title",SqlDbType.VarChar),
			new SqlParameter("@category_id",SqlDbType.Int),
			new SqlParameter("@cnt_content",SqlDbType.VarChar),
			new SqlParameter("@cnt_linkurl",SqlDbType.VarChar),
			new SqlParameter("@cnt_imgurl",SqlDbType.VarChar),
			new SqlParameter("@user_id",SqlDbType.Int),
			new SqlParameter("@cnt_is_lock",SqlDbType.TinyInt),
			new SqlParameter("@cnt_add_time",SqlDbType.DateTime),
			new SqlParameter("@cnt_exp1",SqlDbType.VarChar),
			new SqlParameter("@cnt_exp2",SqlDbType.Int)
			};
			_param[0].Value=0;
			_param[1].Value=_ContentsModel.cnt_id;
			_param[2].Value=_ContentsModel.cnt_title;
			_param[3].Value=_ContentsModel.category_id;
			_param[4].Value=_ContentsModel.cnt_content;
			_param[5].Value=_ContentsModel.cnt_linkurl;
			_param[6].Value=_ContentsModel.cnt_imgurl;
			_param[7].Value=_ContentsModel.user_id;
			_param[8].Value=_ContentsModel.cnt_is_lock;
			_param[9].Value=_ContentsModel.cnt_add_time;
			_param[10].Value=_ContentsModel.cnt_exp1;
			_param[11].Value=_ContentsModel.cnt_exp2;
			res = (int)SqlHelper.ExecuteScalar(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
			return res;
		}
		/// <summary>
		/// 向数据库中插入一条新记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_ContentsModel">Contents实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Create_ContentsInsert(SqlTransaction sp,ContentsEntity _ContentsModel)
		{
			string sqlStr="CreateUpdateDelete_ContentsEntity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@cnt_id",SqlDbType.Int),
			new SqlParameter("@cnt_title",SqlDbType.VarChar),
			new SqlParameter("@category_id",SqlDbType.Int),
			new SqlParameter("@cnt_content",SqlDbType.VarChar),
			new SqlParameter("@cnt_linkurl",SqlDbType.VarChar),
			new SqlParameter("@cnt_imgurl",SqlDbType.VarChar),
			new SqlParameter("@user_id",SqlDbType.Int),
			new SqlParameter("@cnt_is_lock",SqlDbType.TinyInt),
			new SqlParameter("@cnt_add_time",SqlDbType.DateTime),
			new SqlParameter("@cnt_exp1",SqlDbType.VarChar),
			new SqlParameter("@cnt_exp2",SqlDbType.Int)
			};
			_param[0].Value=0;
			_param[1].Value=_ContentsModel.cnt_id;
			_param[2].Value=_ContentsModel.cnt_title;
			_param[3].Value=_ContentsModel.category_id;
			_param[4].Value=_ContentsModel.cnt_content;
			_param[5].Value=_ContentsModel.cnt_linkurl;
			_param[6].Value=_ContentsModel.cnt_imgurl;
			_param[7].Value=_ContentsModel.user_id;
			_param[8].Value=_ContentsModel.cnt_is_lock;
			_param[9].Value=_ContentsModel.cnt_add_time;
			_param[10].Value=_ContentsModel.cnt_exp1;
			_param[11].Value=_ContentsModel.cnt_exp2;
			res = (int)SqlHelper.ExecuteScalar(sp,CommandType.StoredProcedure,sqlStr,_param);
			return res;
		}


		/// <summary>
		/// 向数据表Contents更新一条记录。
		/// </summary>
		/// <param name="_ContentsModel">_ContentsModel</param>
		/// <returns>影响的行数</returns>
		public int Create_ContentsUpdate(ContentsEntity _ContentsModel)
		{
            string sqlStr="CreateUpdateDelete_ContentsEntity";
			SqlParameter[] _param={
				new SqlParameter("@DataAction",SqlDbType.Int),
				new SqlParameter("@cnt_id",SqlDbType.Int),
				new SqlParameter("@cnt_title",SqlDbType.VarChar),
				new SqlParameter("@category_id",SqlDbType.Int),
				new SqlParameter("@cnt_content",SqlDbType.VarChar),
				new SqlParameter("@cnt_linkurl",SqlDbType.VarChar),
				new SqlParameter("@cnt_imgurl",SqlDbType.VarChar),
				new SqlParameter("@user_id",SqlDbType.Int),
				new SqlParameter("@cnt_is_lock",SqlDbType.TinyInt),
				new SqlParameter("@cnt_add_time",SqlDbType.DateTime),
				new SqlParameter("@cnt_exp1",SqlDbType.VarChar),
				new SqlParameter("@cnt_exp2",SqlDbType.Int)
				};
				_param[0].Value=1;
				_param[1].Value=_ContentsModel.cnt_id;
				_param[2].Value=_ContentsModel.cnt_title;
				_param[3].Value=_ContentsModel.category_id;
				_param[4].Value=_ContentsModel.cnt_content;
				_param[5].Value=_ContentsModel.cnt_linkurl;
				_param[6].Value=_ContentsModel.cnt_imgurl;
				_param[7].Value=_ContentsModel.user_id;
				_param[8].Value=_ContentsModel.cnt_is_lock;
				_param[9].Value=_ContentsModel.cnt_add_time;
				_param[10].Value=_ContentsModel.cnt_exp1;
				_param[11].Value=_ContentsModel.cnt_exp2;
			return SqlHelper.ExecuteNonQuery(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
		}
		/// <summary>
		/// 向数据表Contents更新一条记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_ContentsModel">_ContentsModel</param>
		/// <returns>影响的行数</returns>
		public int Create_ContentsUpdate(SqlTransaction sp,ContentsEntity _ContentsModel)
		{
            string sqlStr="CreateUpdateDelete_ContentsEntity";
			SqlParameter[] _param={
				new SqlParameter("@DataAction",SqlDbType.Int),
				new SqlParameter("@cnt_id",SqlDbType.Int),
				new SqlParameter("@cnt_title",SqlDbType.VarChar),
				new SqlParameter("@category_id",SqlDbType.Int),
				new SqlParameter("@cnt_content",SqlDbType.VarChar),
				new SqlParameter("@cnt_linkurl",SqlDbType.VarChar),
				new SqlParameter("@cnt_imgurl",SqlDbType.VarChar),
				new SqlParameter("@user_id",SqlDbType.Int),
				new SqlParameter("@cnt_is_lock",SqlDbType.TinyInt),
				new SqlParameter("@cnt_add_time",SqlDbType.DateTime),
				new SqlParameter("@cnt_exp1",SqlDbType.VarChar),
				new SqlParameter("@cnt_exp2",SqlDbType.Int)
				};
				_param[0].Value=1;
				_param[1].Value=_ContentsModel.cnt_id;
				_param[2].Value=_ContentsModel.cnt_title;
				_param[3].Value=_ContentsModel.category_id;
				_param[4].Value=_ContentsModel.cnt_content;
				_param[5].Value=_ContentsModel.cnt_linkurl;
				_param[6].Value=_ContentsModel.cnt_imgurl;
				_param[7].Value=_ContentsModel.user_id;
				_param[8].Value=_ContentsModel.cnt_is_lock;
				_param[9].Value=_ContentsModel.cnt_add_time;
				_param[10].Value=_ContentsModel.cnt_exp1;
				_param[11].Value=_ContentsModel.cnt_exp2;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.StoredProcedure,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表Contents中的一条记录
		/// </summary>
	    /// <param name="cnt_id">cnt_id</param>
		/// <returns>影响的行数</returns>
		public int Create_ContentsDelete(int cnt_id)
		{
        ContentsEntity _ContentsModel =new ContentsEntity();
			string sqlStr="CreateUpdateDelete_ContentsEntity";
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.VarChar),
			new SqlParameter("@cnt_id",SqlDbType.Int),			
			new SqlParameter("@cnt_title",SqlDbType.VarChar),
			new SqlParameter("@category_id",SqlDbType.Int),
			new SqlParameter("@cnt_content",SqlDbType.VarChar),
			new SqlParameter("@cnt_linkurl",SqlDbType.VarChar),
			new SqlParameter("@cnt_imgurl",SqlDbType.VarChar),
			new SqlParameter("@user_id",SqlDbType.Int),
			new SqlParameter("@cnt_is_lock",SqlDbType.TinyInt),
			new SqlParameter("@cnt_add_time",SqlDbType.DateTime),
			new SqlParameter("@cnt_exp1",SqlDbType.VarChar),
			new SqlParameter("@cnt_exp2",SqlDbType.Int)
			};
			_param[0].Value=2;
			_param[1].Value=cnt_id;
			_param[2].Value=_ContentsModel.cnt_title;
			_param[3].Value=_ContentsModel.category_id;
			_param[4].Value=_ContentsModel.cnt_content;
			_param[5].Value=_ContentsModel.cnt_linkurl;
			_param[6].Value=_ContentsModel.cnt_imgurl;
			_param[7].Value=_ContentsModel.user_id;
			_param[8].Value=_ContentsModel.cnt_is_lock;
			_param[9].Value=_ContentsModel.cnt_add_time;
			_param[10].Value=_ContentsModel.cnt_exp1;
			_param[11].Value=_ContentsModel.cnt_exp2;
			return SqlHelper.ExecuteNonQuery(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表Contents中的一条记录,带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
	    /// <param name="cnt_id">cnt_id</param>
		/// <returns>影响的行数</returns>
		public int Create_ContentsDelete(SqlTransaction sp,int cnt_id)
		{
         ContentsEntity _ContentsModel =new ContentsEntity();
			string sqlStr="CreateUpdateDelete_ContentsEntity";
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@cnt_id",SqlDbType.Int),
			new SqlParameter("@cnt_title",SqlDbType.VarChar),
			new SqlParameter("@category_id",SqlDbType.Int),
			new SqlParameter("@cnt_content",SqlDbType.VarChar),
			new SqlParameter("@cnt_linkurl",SqlDbType.VarChar),
			new SqlParameter("@cnt_imgurl",SqlDbType.VarChar),
			new SqlParameter("@user_id",SqlDbType.Int),
			new SqlParameter("@cnt_is_lock",SqlDbType.TinyInt),
			new SqlParameter("@cnt_add_time",SqlDbType.DateTime),
			new SqlParameter("@cnt_exp1",SqlDbType.VarChar),
			new SqlParameter("@cnt_exp2",SqlDbType.Int)
			};
			_param[0].Value=2;
			_param[1].Value=cnt_id;
			_param[2].Value=_ContentsModel.cnt_title;
			_param[3].Value=_ContentsModel.category_id;
			_param[4].Value=_ContentsModel.cnt_content;
			_param[5].Value=_ContentsModel.cnt_linkurl;
			_param[6].Value=_ContentsModel.cnt_imgurl;
			_param[7].Value=_ContentsModel.user_id;
			_param[8].Value=_ContentsModel.cnt_is_lock;
			_param[9].Value=_ContentsModel.cnt_add_time;
			_param[10].Value=_ContentsModel.cnt_exp1;
			_param[11].Value=_ContentsModel.cnt_exp2;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.StoredProcedure,sqlStr,_param);
		}
		#endregion
		
		#region 数据实体
		/// <summary>
		/// 得到  contents 数据实体
		/// </summary>
		/// <param name="row">row</param>
		/// <returns>contents 数据实体</returns>
		public ContentsEntity Populate_ContentsEntity_FromDr(DataRow row)
		{
			ContentsEntity Obj = new ContentsEntity();
			if(row!=null)
			{
				Obj.cnt_id = (( row["cnt_id"])==DBNull.Value)?0:Convert.ToInt32( row["cnt_id"]);
				Obj.cnt_title =  row["cnt_title"].ToString();
				Obj.category_id = (( row["category_id"])==DBNull.Value)?0:Convert.ToInt32( row["category_id"]);
				Obj.cnt_content =  row["cnt_content"].ToString();
				Obj.cnt_linkurl =  row["cnt_linkurl"].ToString();
				Obj.cnt_imgurl =  row["cnt_imgurl"].ToString();
				Obj.user_id = (( row["user_id"])==DBNull.Value)?0:Convert.ToInt32( row["user_id"]);
				Obj.cnt_is_lock = (( row["cnt_is_lock"])==DBNull.Value)?Convert.ToByte(0):Convert.ToByte( row["cnt_is_lock"]);
				Obj.cnt_add_time = (( row["cnt_add_time"])==DBNull.Value)?Convert.ToDateTime(1900-1-1):Convert.ToDateTime( row["cnt_add_time"]);
				Obj.cnt_exp1 =  row["cnt_exp1"].ToString();
				Obj.cnt_exp2 = (( row["cnt_exp2"])==DBNull.Value)?0:Convert.ToInt32( row["cnt_exp2"]);
			}
			else
			{
				return null;
			}
			return Obj;
		}
		
        /// <summary>
		/// 得到  contents 数据实体
		/// </summary>
		/// <param name="dr">dr</param>
		/// <returns>contents 数据实体</returns>
		public ContentsEntity Populate_ContentsEntity_FromDr(IDataReader dr)
		{
			ContentsEntity Obj = new ContentsEntity();
			
				Obj.cnt_id = (( dr["cnt_id"])==DBNull.Value)?0:Convert.ToInt32( dr["cnt_id"]);
				Obj.cnt_title =  dr["cnt_title"].ToString();
				Obj.category_id = (( dr["category_id"])==DBNull.Value)?0:Convert.ToInt32( dr["category_id"]);
				Obj.cnt_content =  dr["cnt_content"].ToString();
				Obj.cnt_linkurl =  dr["cnt_linkurl"].ToString();
				Obj.cnt_imgurl =  dr["cnt_imgurl"].ToString();
				Obj.user_id = (( dr["user_id"])==DBNull.Value)?0:Convert.ToInt32( dr["user_id"]);
				Obj.cnt_is_lock = (( dr["cnt_is_lock"])==DBNull.Value)?Convert.ToByte(0):Convert.ToByte( dr["cnt_is_lock"]);
				Obj.cnt_add_time = (( dr["cnt_add_time"])==DBNull.Value)?Convert.ToDateTime(1900-1-1):Convert.ToDateTime( dr["cnt_add_time"]);
				Obj.cnt_exp1 =  dr["cnt_exp1"].ToString();
				Obj.cnt_exp2 = (( dr["cnt_exp2"])==DBNull.Value)?0:Convert.ToInt32( dr["cnt_exp2"]);
			
			return Obj;
		}
		#endregion
		
		/// <summary>
		/// 根据ID,返回一个Contents对象
		/// </summary>
		/// <param name="cnt_id">cnt_id</param>
		/// <returns>Contents对象</returns>
		public ContentsEntity Get_ContentsEntity (int cnt_id)
		{
			ContentsEntity _obj=null;
			SqlParameter[] _param={
			new SqlParameter("@cnt_id",SqlDbType.Int)
			};
			_param[0].Value=cnt_id;
			string sqlStr="select * from Contents where cnt_id=@cnt_id";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(Conn.SqlConn,CommandType.Text,sqlStr,_param))
			{
				while(dr.Read())
				{
            		_obj=Populate_ContentsEntity_FromDr(dr);
				}
			}
			return _obj;
		}
		/// <summary>
		/// 得到数据表Contents所有记录
		/// </summary>
		/// <returns>数据集</returns>
		public IList< ContentsEntity> Get_ContentsAll()
		{
			IList< ContentsEntity> Obj=new List< ContentsEntity>();
			string sqlStr="select * from Contents";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(Conn.SqlConn,CommandType.Text,sqlStr))
			{
				while(dr.Read())
				{
					Obj.Add(Populate_ContentsEntity_FromDr(dr));
				}
			}
			return Obj;
		}
		/// <summary>
        /// 检测是否存在根据主键
        /// </summary>
        /// <param name="cnt_id">cnt_id</param>
        /// <returns>是/否</returns>
		public bool IsExistContents(int cnt_id)
		{
			SqlParameter[] _param={
                                      new SqlParameter("@cnt_id",SqlDbType.Int)
                                  };
            _param[0].Value=cnt_id;
            string sqlStr="select Count(*) from Contents where cnt_id=@cnt_id";
            int a=Convert.ToInt32(SqlHelper.ExecuteScalar(Conn.SqlConn,CommandType.Text,sqlStr,_param));
            if(a>0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

        #endregion
		
		#region----------自定义实例化接口函数----------
		#endregion
    }
}
